package com.jbm.jbmsupplier.zp.sqldata;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jbm.jbmsupplier.zp.entity.Buddy_ListData;
import com.jbm.jbmsupplier.zp.entity.ChatMsgEntity;
import com.jbm.jbmsupplier.zp.entity.Recent_ContactsData;
import com.jbm.jbmsupplier.zp.interfaces.SolistGroads;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by donar_work on 2016/11/9.
 */

public class MySqliteUtlis {
    private static MyMySQLiteHelper mysqlis;
    private static SQLiteDatabase db;
    private static SolistGroads solistGroads;

    private MySqliteUtlis() {
    }
    public static MySqliteUtlis GetInstan(Context context) {
        if (mysqlis==null){
        mysqlis = new MyMySQLiteHelper(context);
        if (mysqlis != null)
            db = mysqlis.getWritableDatabase();
        }
        if (solistGroads==null){
            solistGroads=new SolistGroads();
        }
        return MySingletUtlis.mySqliteUtlis;
    }

    public  static class MySingletUtlis{//第一次他会先初始化这个类
        private   final static MySqliteUtlis mySqliteUtlis=new MySqliteUtlis();
    }

    //添加个人群主lz

    public void InsertGroad( String tableName, String name,String head_portrait,String time,String msg,int unreadmsg,int cgid,int tag,int toid){
        solistGroads.insertGroadtable( db,  tableName,  name , head_portrait ,  time ,  msg, unreadmsg, cgid, tag, toid);
    }
    //添加群信息lz
    public void InsertGroadTable(String tableName, String name ,String head_portrait ,String  date ,String  texts,String  time ,int to_id , int local_id,int orderid,double amount ,String sedname ,String Route,String MediaName ,String AudioTime ,int UnreadMsg ,String address ,String photo , int flag ,int cgid,int tags,String ISboo,int login){
        solistGroads.insertGroadTable( db,  tableName,  name , head_portrait ,  date ,  texts,  time , to_id ,  local_id ,orderid,amount, sedname , Route, MediaName , AudioTime , UnreadMsg , address ,photo, flag , cgid,tags,ISboo,login);
    }
    //查找消息lz
    public ArrayList<ChatMsgEntity> ShowGroadTable(String tableName, int cgid,int login) {
        return   solistGroads.showGroadTable(db,tableName,cgid,login);

    }
    //查找群名字lz
    public Recent_ContactsData SelectName(String tableName,int cgid){
        return   solistGroads. showGroadTableid(db, tableName,cgid);
    }

    //查找所有群lz
    public ArrayList<Recent_ContactsData> ShowGroadTables(String tableName, int cgid) {
        return   solistGroads.showGroad(db,tableName,cgid);

    }
    //删除每一个群lz
    public void setdelete(String tableName,int cgid,int loginid){
        solistGroads.setdelete(db,tableName,cgid,loginid);

    }
    //查找未读消息lz
    public int Selectreadmsg(String tableName, int cgid){
        return   solistGroads.SelectUnreadmsg(db,tableName,cgid);
    }
    //更新未读消息lz
    public void UpdateReadmsg(String tableName, int cgid){
        solistGroads.UpdateReadmsg(db,tableName,cgid);
    }

    //更改发送失败获成功消息
    public void UpdateErrMsg(String tableName, int cgid,String data,String Isboos,int login){
        solistGroads.UpdateErrorMsg(db,tableName,Isboos,cgid,data, login);
    }
    //存储数据，存储最近浏览设备
//    public void Storingdata(String Name, String DeviceID, String PTZType, String DeviceType, String ParentID, String BrowseTime){
//         db.execSQL("insert into "+SQUSER+" (Name,DeviceID,PTZType,DeviceType,ParentID,BrowseTime) values (?,?,?,?,?,?)",new String[]{Name,DeviceID,PTZType,DeviceType,ParentID,BrowseTime});
//    }

    /**
     *   存储人员信息列表数据
     * @param tableName 表名
     * @param m_account_id 人员ID
     * @param nickname 昵称
     * @param account
     * @param mobile 手机号
     * @param remark 备注
     * @param head_portrait 头像
     * @param autograph 签名
     * @param region  地区
     * @param Circle_friends 朋友圈
     * @param isfriends 是否为好友标识 0:否,1:是
     *获取好友的数据好友
     */
    public void StoringBuddydata(String tableName, int m_account_id,String nickname,String account,String mobile,String remark,String head_portrait,String autograph,String region,String Circle_friends,int isfriends,int loginid) {
        solistGroads.StoringBuddydata(db,tableName,m_account_id,nickname,account,mobile,remark,head_portrait,autograph,region,Circle_friends,isfriends,loginid);
    }
    //存储聊天记录数据
    /**
     *
     * @param tableName 表名
     * @param myhead_portrait 头像
     * @param to_id 好友ID
     * @param text 发送消息文本
     * @param Date 发送接收消息时间
     * @param Name 名字
     * @param Route 图片
     * @param MediaName 语音
     * @param AudioTime 语音时长
     * @param payDate 支付时间
     * @param payMoney 支付金额
     * @param payMode 支付方式
     * @param payState 支付状态
     * @param flag 布局标识 0-X
     * @param UnreadMsg 已读未读标识 0:未读,1:已读
     * @param code 提货码
     * @param memo 提货码备注
     * @param isboolean 发送消息成功或失败 :失败,false是失败true 是成功
     *   保存聊天记录
     */
    public void StoringChatData(String tableName, int to_id,String myhead_portrait, String text, String Date, String Name,String Route,String MediaName,String AudioTime,String payDate,String payMoney,String payMode,String payState,String code,String memo, int flag, int UnreadMsg,String isboolean,int loginid) {
        solistGroads.StoringChatData(db,tableName,to_id,myhead_portrait,text,Date,Name,Route,MediaName,AudioTime,payDate,payMoney,payMode,payState,code,memo,flag,UnreadMsg,isboolean,loginid);
    }
    //存储最近联系人
    public void StoringRecentID(String tableName, int contactsID,int login,String date) {
        solistGroads.StoringRecentID(db,tableName,contactsID,login,date);
    }
    //根据loginid查找这个人在不在联系列表里最近联系人ID
    public ArrayList<String> showaRecentContactsid(String tableName,int toid,int loginin) {
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        return solistGroads.showaRecentContactsid(db,tableName,toid,loginin);
    }
    //根据login查找所有最近联系人
    public ArrayList<String> showaRecentContactsids(String tableName,int login) {
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        return solistGroads. showaRecentContactsids(db,tableName,login);
    }

    //查询聊天最后一条数据在哪联系人页面
    public ArrayList<Recent_ContactsData> showRecentContactslast(String tableName,int loginid,int toid) {
        return solistGroads.showRecentContactslast(db,tableName,loginid,toid);
    }


    //查找聊天数据
    public ArrayList<ChatMsgEntity> showChatData(String tableName,int loginid,int toid) {

        return solistGroads.showChatData(db,tableName,loginid,toid);
    }


    //查找所有好友数据 isfriends = 1为好友好友表
    public ArrayList<Buddy_ListData> showdata(String tableName,int loginid) {
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        return solistGroads.showdata(db,tableName,loginid);

    }


    //根据ID查找人员信息
    public Buddy_ListData showFriendsToID(String tableName,int m_account_id,int loginid){

        return solistGroads.showFriendsToID(db,tableName,m_account_id,loginid);
    }






    // 删除最近联系人ID
    public void deletedata(String tableName, String contactsID,int login) {
        db.execSQL("delete from " + tableName + " where contactsID=? and login = ?", new String[]{contactsID,login+""});
    }
    // 删除好友数据
    public void deleteFriendsdata(String fridlisttableName,String chatstableName, String m_account_id,int loginid) {
        db.execSQL("delete from " + fridlisttableName + " where m_account_id=? and loginid= ?", new Object[]{m_account_id,loginid+""});
        db.execSQL("delete from " + chatstableName + " where to_id = ? and loginid = ?",new Object[]{m_account_id,loginid+""});
    }
    // 根据好友id删除本地好友
    public void deleteUserid(String table,int loginid) {
        db.execSQL("delete from "+table+" where loginid = ?", new String[] { loginid +""});
    }
    //更新聊天未读数据
    public void Update(String tableName,int loginid) {
        db.execSQL("update " + tableName + " set UnreadMsg = '1' where UnreadMsg = '0' and loginid = ?",new String[]{loginid+""});
    }

    //更新聊天发送是不成功数据
    public void Updateer(String tableName,String isboolean,String Date,int loginid) {
        db.execSQL("update " + tableName + " set isboolean = '"+isboolean+"'"+" where Date='"+Date+"' and loginid ='"+loginid+"'");
    }

    //更新好友数据
    public void UpdateIsfriends(String tableName,int m_account_id) {
        db.execSQL("update " + tableName + " set isfriends = '1' where m_account_id = "+m_account_id);
    }


    //查询未读消息条数
    public int ChatUnreadMsg(String tableName,int loginid,int toid){
        int UnreadMsg;
        Cursor cursor = db.rawQuery("select count(UnreadMsg) from "+tableName+" where UnreadMsg = '0' and to_id= ? and loginid = ? ",new String[]{toid+"",loginid+""});
        cursor.moveToFirst();
        UnreadMsg = cursor.getInt(0);
        return UnreadMsg;
    }

    //查找好友ID好友表
    public ArrayList<HashMap<String,String>> showalldata(String tableName,int m_account_id) {
        ArrayList<HashMap<String,String>> list = new ArrayList<>();
        Buddy_ListData buddy_listdata = null;
        Cursor cursor = db.rawQuery("select * from " + tableName+" where m_account_id = "+m_account_id, null);//m_account_id
        cursor.moveToPrevious();
        //获取name列的索引
        while (cursor.moveToNext()) {
            HashMap<String,String> map=new HashMap<>();
            int in = cursor.getInt(cursor.getColumnIndex("m_account_id"));
            map.put("id",in+"");
            list.add(map);
        }
//        cursor.close();//关闭结果集
//        db.close();//关闭数据库对象
        return list;

    }
}
